package com.bunnyhsu.controller;

import com.bunnyhsu.dto.Result;
import com.bunnyhsu.enums.FailEnum;
import com.bunnyhsu.service.AdminService;
import com.bunnyhsu.service.SuggestService;
import com.bunnyhsu.service.TypeService;
import com.bunnyhsu.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by xl on 2018/4/7.
 */

@RestController
@CrossOrigin("*")
public class SuggestController {

    @Autowired
    private SuggestService suggestService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private TypeService typeService;


    @PostMapping("/getUnauditedSuggest")
    public Result getUnauditedSuggest(int page, int number, String token) {

        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else
        {
            if(page <= 0 || number <= 0)
            {
                return ResultUtil.fail(FailEnum.NUMBER_UNSUIT.getId(),FailEnum.NUMBER_UNSUIT.getMsg());
            }
            else
            {
                return ResultUtil.success(suggestService.getUnauditedSuggests(page, number), "");
            }
        }
    }

    @PostMapping("/getAuditedSuggest")
    public Result getAuditedSuggest(int page, int number, String token) {

        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else
        {
            if(page <= 0 || number <= 0)
            {
                return ResultUtil.fail(FailEnum.NUMBER_UNSUIT.getId(),FailEnum.NUMBER_UNSUIT.getMsg());
            }
            else
            {
                return ResultUtil.success(suggestService.getAuditedSuggests(page, number), "");
            }
        }
    }

    @PostMapping("/getRepliedSuggest")
    public Result getRepliedSuggest(int page, int number, String token) {

        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else
        {
            if(page <= 0 || number <= 0)
            {
                return ResultUtil.fail(FailEnum.NUMBER_UNSUIT.getId(),FailEnum.NUMBER_UNSUIT.getMsg());
            }
            else
            {
                return ResultUtil.success(suggestService.getRepliedSuggests(page, number), "");
            }
        }
    }

    @PostMapping("setSuggest2Replied")
    public Result setSuggest2Replied(String reply, int typeId, int id, String token)
    {
        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else if("".equals(typeService.getTypeById(typeId)))
        {
            return ResultUtil.fail(FailEnum.TYPE_NONEXIST.getId(),FailEnum.TYPE_NONEXIST.getMsg());
        }
        else
        {
            if(suggestService.updateSuggest2Replied(reply,typeId,id))
            {
                return ResultUtil.success( "", "成功");
            }
            else
            {
                return ResultUtil.fail(FailEnum.SUGGEST_NONEXIST.getId(),FailEnum.SUGGEST_NONEXIST.getMsg());
            }
        }
    }

    @PostMapping("setSuggest2SceneReplied")
    public Result setSuggest2SceneReplied( int typeId, int id, String token)
    {
        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else if("".equals(typeService.getTypeById(typeId)))
        {
            return ResultUtil.fail(FailEnum.TYPE_NONEXIST.getId(),FailEnum.TYPE_NONEXIST.getMsg());
        }
        else
        {
            if(suggestService.updateSuggest2SceneReplied(typeId, id))
            {
                return ResultUtil.success( "", "成功");
            }
            else
            {
                return ResultUtil.fail(FailEnum.SUGGEST_NONEXIST.getId(),FailEnum.SUGGEST_NONEXIST.getMsg());
            }
        }
    }

    @PostMapping("setSuggest2Audited")
    public Result setSuggest2Audited(String reply, int typeId, int id, String token)
    {
        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else if("".equals(typeService.getTypeById(typeId)))
        {
            return ResultUtil.fail(FailEnum.TYPE_NONEXIST.getId(),FailEnum.TYPE_NONEXIST.getMsg());
        }
        else
        {
            if(suggestService.updateSuggest2Audited(reply,typeId,id))
            {
                return ResultUtil.success( "", "成功");
            }
            else
            {
                return ResultUtil.fail(FailEnum.SUGGEST_NONEXIST.getId(),FailEnum.SUGGEST_NONEXIST.getMsg());
            }
        }
    }

    @PostMapping("/getRepliedSuggestByDate")
    public Result getRepliedSuggestByDate(int page, int number, String down, String up, String token) {

        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else
        {
            if(page <= 0 || number <= 0)
            {
                return ResultUtil.fail(FailEnum.NUMBER_UNSUIT.getId(),FailEnum.NUMBER_UNSUIT.getMsg());
            }
            else
            {
                return ResultUtil.success(suggestService.getRepliedSuggestsByDate(page,number,up,down), "");
            }
        }
    }
    @PostMapping("/getAuditedSuggestByDate")
    public Result getAuditedSuggestByDate(int page, int number, String down, String up, String token) {

        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else
        {
            if(page <= 0 || number <= 0)
            {
                return ResultUtil.fail(FailEnum.NUMBER_UNSUIT.getId(),FailEnum.NUMBER_UNSUIT.getMsg());
            }
            else
            {
                return ResultUtil.success(suggestService.getAuditedSuggestsByDate(page,number,up,down), "");
            }
        }
    }
    @PostMapping("/getUnauditedSuggestByDate")
    public Result getUnauditedSuggestByDate(int page, int number, String down, String up, String token) {

        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else
        {
            if(page <= 0 || number <= 0)
            {
                return ResultUtil.fail(FailEnum.NUMBER_UNSUIT.getId(),FailEnum.NUMBER_UNSUIT.getMsg());
            }
            else
            {
                return ResultUtil.success(suggestService.getUnauditedSuggestsByDate(page,number,up,down), "");
            }
        }
    }

    @PostMapping("/getRepliedSuggestByDateAndType")
    public Result getRepliedSuggestByDateAndType(int page, int number, String up, String down, int typeId, String token)
    {
        Result result = checkToken(token);
        if(result != null)
        {
            return result;
        }
        else
        {
            if(page <= 0 || number <= 0)
            {
                return ResultUtil.fail(FailEnum.NUMBER_UNSUIT.getId(),FailEnum.NUMBER_UNSUIT.getMsg());
            }
            else
            {
                return ResultUtil.success(suggestService.getUnionSuggestByDateAndType(page,number,up,down,typeId), "");
            }
        }
    }

    private Result checkToken(String token)
    {
        if(token == null)
        {
            return ResultUtil.fail(FailEnum.NO_ANTH.getId(), FailEnum.NO_ANTH.getMsg());
        }
        else if (adminService.comfirmToken(token))
        {
            return null;
        }
        else
        {
            return ResultUtil.fail(FailEnum.TOKEN_WRONG.getId(), FailEnum.TOKEN_WRONG.getMsg());
        }
    }
}
